package com.zr.util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * @author zr
 * @version 1.0
 * @date 2021/4/23 10:13
 */
public class WaterMark {

    public static void main(String[] args) {
        //主页面
        JFrame a = new JFrame("图片批量加水印 v0.02   --author zr  ");
        a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        a.setLocation(100, 100);
        a.setSize(471, 400);
        a.setLayout(new FlowLayout());

        //图片包位置
        JLabel b = new JLabel("图片包地址：");
        JTextField c = new JTextField("");
        c.setText("");
        c.setPreferredSize(new Dimension(250, 30));
        //水印图片位置
        JLabel b2 = new JLabel("水印地址：");
        JTextField c2 = new JTextField("");
        c2.setText("d:/1.png");
        c2.setPreferredSize(new Dimension(250, 30));


        //加水印后图片包位置
        JLabel b4 = new JLabel("目的图片包地址：");
        JTextField c3 = new JTextField("");
        c3.setText("");
        c3.setPreferredSize(new Dimension(250, 30));


        //添加水印按钮
        JButton button = new JButton("添加水印");
        button.setBounds(50, 50, 250, 30);//设置按钮在容器中的位置
        TextArea textArea = new TextArea();
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ImageWatermark imageWatermark = new ImageWatermark();
                imageWatermark.main(c.getText(), textArea, c2.getText(),c3.getText());

            }
        });





        JLabel b3 = new JLabel("author zr  contact with me qq:2437264464");


        a.add(b);//图片包labal
        a.add(c);//图片包输入框
        createUI(a,c);

        a.add(b2);//水印labal
        a.add(c2);//水印输入框
        createUI(a,c2);

        a.add(b4);//目的地址labal
        a.add(c3);//目的地址输入框
        createUI(a,c3);

        a.add(button);//将按钮加在容器上
        a.add(textArea);//日志文本显示
        a.add(b3);


        a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        a.setResizable(false);
        a.setVisible(true);
    }




    private static void createUI(final JFrame frame,JTextField jTextField){
        JPanel panel = new JPanel();
        LayoutManager layout = new FlowLayout();
        panel.setLayout(layout);

        JButton button = new JButton("```");
        button.setBounds(100, 100, 20, 20);
        final JLabel label = new JLabel();


        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                //默认目录
                String defaultDirectory = "d:/";
                JFileChooser fileChooser = new JFileChooser();


                fileChooser.setPreferredSize(new Dimension(800, 800));
                fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                fileChooser.setCurrentDirectory(new File(defaultDirectory));

                int option = fileChooser.showOpenDialog(null);
                if(option == JFileChooser.APPROVE_OPTION){
                    File file = fileChooser.getSelectedFile();
//                    label.setText("选择的目录是: " + file.getName());
                    jTextField.setText(file.getPath());
                }
            }
        });

        panel.add(button);
        panel.add(label);
        frame.getContentPane().add(panel, BorderLayout.CENTER);
    }


}
